/**
 * Filename: RecyclerViewUtils.java
 * @author  Firefly
 * @contact postfirefly@163.com
 * @time    2016/4/18 15:14
 */
package com.github.postsanf.yinian.ynlist;
import android.support.v7.widget.RecyclerView;
import android.view.View;

public class RecyclerViewUtils{
	/**
	 * 设置HeaderView
	 * @param recyclerView
	 * @param view
	 */
	public static void setHeaderView(RecyclerView recyclerView, View view) {
		RecyclerView.Adapter outerAdapter = recyclerView.getAdapter();

		if (outerAdapter == null || !(outerAdapter instanceof YNRecyclerViewAdapter)) {
			return;
		}

		YNRecyclerViewAdapter headerAndFooterAdapter = (YNRecyclerViewAdapter) outerAdapter;
		if (headerAndFooterAdapter.getHeaderViewsCount() == 0) {
			headerAndFooterAdapter.addHeaderView(view);
		}
	}

	/**
	 * 设置FooterView
	 *
	 * @param recyclerView
	 * @param view
	 */
	public static void setFooterView(RecyclerView recyclerView, View view) {
		RecyclerView.Adapter outerAdapter = recyclerView.getAdapter();

		if (outerAdapter == null || !(outerAdapter instanceof YNRecyclerViewAdapter)) {
			return;
		}

		YNRecyclerViewAdapter headerAndFooterAdapter = (YNRecyclerViewAdapter) outerAdapter;
		if (headerAndFooterAdapter.getFooterViewsCount() == 0) {
			headerAndFooterAdapter.addFooterView(view);
		}
	}

	/**
	 * 移除FooterView
	 *
	 * @param recyclerView
	 */
	public static void removeFooterView(RecyclerView recyclerView) {

		RecyclerView.Adapter outerAdapter = recyclerView.getAdapter();

		if (outerAdapter != null && outerAdapter instanceof YNRecyclerViewAdapter) {

			int footerViewCounter = ((YNRecyclerViewAdapter) outerAdapter).getFooterViewsCount();
			if (footerViewCounter > 0) {
				View footerView = ((YNRecyclerViewAdapter) outerAdapter).getFooterView();
				((YNRecyclerViewAdapter) outerAdapter).removeFooterView(footerView);
			}
		}
	}

	/**
	 * 移除HeaderView
	 *
	 * @param recyclerView
	 */
	public static void removeHeaderView(RecyclerView recyclerView) {

		RecyclerView.Adapter outerAdapter = recyclerView.getAdapter();

		if (outerAdapter != null && outerAdapter instanceof YNRecyclerViewAdapter) {

			int headerViewCounter = ((YNRecyclerViewAdapter) outerAdapter).getHeaderViewsCount();
			if (headerViewCounter > 0) {
				View headerView = ((YNRecyclerViewAdapter) outerAdapter).getHeaderView();
				((YNRecyclerViewAdapter) outerAdapter).removeFooterView(headerView);
			}
		}
	}

	/**
	 * 请使用本方法替代RecyclerView.ViewHolder的getLayoutPosition()方法
	 *
	 * @param recyclerView
	 * @param holder
	 * @return
	 */
	public static int getLayoutPosition(RecyclerView recyclerView, RecyclerView.ViewHolder holder) {
		RecyclerView.Adapter outerAdapter = recyclerView.getAdapter();
		if (outerAdapter != null && outerAdapter instanceof YNRecyclerViewAdapter) {

			int headerViewCounter = ((YNRecyclerViewAdapter) outerAdapter).getHeaderViewsCount();
			if (headerViewCounter > 0) {
				return holder.getLayoutPosition() - headerViewCounter;
			}
		}

		return holder.getLayoutPosition();
	}

	/**
	 * 请使用本方法替代RecyclerView.ViewHolder的getAdapterPosition()方法
	 *
	 * @param recyclerView
	 * @param holder
	 * @return
	 */
	public static int getAdapterPosition(RecyclerView recyclerView, RecyclerView.ViewHolder holder) {
		RecyclerView.Adapter outerAdapter = recyclerView.getAdapter();
		if (outerAdapter != null && outerAdapter instanceof YNRecyclerViewAdapter) {

			int headerViewCounter = ((YNRecyclerViewAdapter) outerAdapter).getHeaderViewsCount();
			if (headerViewCounter > 0) {
				return holder.getAdapterPosition() - headerViewCounter;
			}
		}

		return holder.getAdapterPosition();
	}
}